/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.JPanel;

public abstract class Chart extends JPanel implements MouseListener {
	public static final int CHART_AREA = 0;

	private int width= 0,height = 0;
	private ChartDataSelectionListener chart_listener =null;
	private Number min_n=null,max_n=null;
	private Comparable min_c= null,max_c = null;

	protected int heading_size = 12,other_size= 9;
	private float spacer = 0.05f,axis_offset=5;
	//private ChartPanel chartpanel = null;

	private Chart(
	   VectorVectorChartData vvcd,
	   ChartDataSelectionListener chart_listener,
	   int width,int height,
		boolean show_legend,
	   String heading,String sub,String x_head,String y_head
	) {
		this.chart_listener = chart_listener;
		this.width = width;
		this.height = height;
		if (width * height < 64000) {
			heading_size = 10;
			other_size = 8;
			spacer = 0.02f;
			axis_offset=2;
		} else if (width * height < 307200) {
			heading_size = 12;
			other_size = 10;
			spacer = 0.05f;
			axis_offset=5;
		} else {
			heading_size = 14;
			other_size = 10;
			spacer = 0.05f;
			axis_offset=5;
		}
//		JFreeChart freechart = createChart(vvcd.toDataSet(),heading,sub,x_head,y_head,show_legend,spacer,axis_offset);
//		chartpanel = new ChartPanel(freechart);
//		chartpanel.setPreferredSize(new Dimension(width, height));
//		chartpanel.setEnforceFileExtensions(false);
//		if (chart_listener!= null) {
//			chartpanel.addMouseListener(this);
//		}
		setLayout(new BorderLayout());
//		JScrollPane scroll =new JScrollPane(chartpanel);
//		add(scroll,BorderLayout.CENTER);
	}

//	abstract JFreeChart createChart(
//	   CategoryDataset categorydataset,
//	   String heading,String sub,String x_head,String y_head,
//		boolean legend,
//	   float spacer,float axis_offset
//	);

	public void mouseClicked(MouseEvent e) {
//		String tip = chartpanel.getToolTipText(e);
//		if (tip != null) {
//			chart_listener.selected(
//			   new Double(tip.substring(tip.indexOf("=")+1,tip.length())),
//			   tip.substring(tip.lastIndexOf(",")+2,tip.indexOf(")")),
//			   tip.substring(1,tip.lastIndexOf(","))
//			);
//		}
	}

	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}


//	public static class ChartArea extends Chart {
//		public ChartArea(
//		   VectorVectorChartData vvcd,
//		   ChartDataSelectionListener chart_listener,
//		   int width,int height,
//			boolean show_legend,
//		   String heading,String sub,String x_head,String y_head
//		) {
//			super(vvcd,chart_listener,width,height,show_legend,heading,sub,x_head,y_head);
//		}

//		public JFreeChart createChart(
//		   CategoryDataset categorydataset,
//		   String heading,String sub,String x_head,String y_head,
//		   boolean legend,
//		   float spacer,float axis_offset
//		) {
//			JFreeChart jfreechart = ChartFactory.createAreaChart(
//			                           heading, y_head, x_head,
//			                           categorydataset,
//			                           PlotOrientation.VERTICAL, legend,
//			                           true, false);
//			if (legend) {
//				StandardLegend standardlegend = (StandardLegend) jfreechart.getLegend();
//				standardlegend.setAnchor(3);
//			};
//			jfreechart.setBackgroundPaint(Color.white);
//			if (sub != null) {
//				TextTitle texttitle = (new TextTitle (sub));
//				texttitle.setFont(new Font("SansSerif", 0, other_size));
//				texttitle.setPosition(RectangleEdge.TOP);
//				texttitle.setSpacer(new Spacer(0, spacer, spacer, spacer, spacer));
//				texttitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
//				jfreechart.addSubtitle(texttitle);
//			}
//			CategoryPlot categoryplot = jfreechart.getCategoryPlot();
//			categoryplot.setForegroundAlpha(0.5F);
//			categoryplot.setAxisOffset(new Spacer(1, axis_offset, axis_offset, axis_offset, axis_offset));
//			categoryplot.setBackgroundPaint(Color.lightGray);
//			categoryplot.setDomainGridlinesVisible(true);
//			categoryplot.setDomainGridlinePaint(Color.white);
//			categoryplot.setRangeGridlinesVisible(true);
//			categoryplot.setRangeGridlinePaint(Color.white);
//			CategoryAxis categoryaxis = categoryplot.getDomainAxis();
//			categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
//			categoryaxis.setLowerMargin(0.0);
//			categoryaxis.setUpperMargin(0.0);
//			//categoryaxis.addCategoryLabelToolTip("Type 1", "The first type.");
//			//categoryaxis.addCategoryLabelToolTip("Type 2", "The second type.");
//			//categoryaxis.addCategoryLabelToolTip("Type 3", "The third type.");
//			NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
//			numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//			numberaxis.setLabelAngle(0.0);
//			return jfreechart;
//		}
//
//	}

	public static class ChartData {
		public Number value = null;
		public Comparable column = null;
		public ChartData(Number value,Comparable column) {
			this.value = value;
			this.column= column;
		}
	}

	public interface ChartDataSelectionListener {
		public void selected(Number value,Comparable column,Comparable series);
	}

	public static class VectorChartData {
		private Vector store=new Vector();
		public Comparable name = null;

		public VectorChartData() {}
		public VectorChartData(Comparable name){
			this.name =name;
		}

		public ChartData get(int pos){
			return (ChartData)store.get(pos);
		}

		public void add(ChartData item){
			store.add(item);
		}

		public boolean remove(ChartData item){
			return store.remove(item);
		}

		public ChartData remove(int pos){
			return (ChartData) store.remove(pos);
		}

		public int size(){
			return store.size();
		}
	}

	public static class VectorVectorChartData {
		private Vector store=new Vector();

		public VectorVectorChartData() {}
		public VectorVectorChartData(VectorChartData vcd) {
			add(vcd);
		}

		public VectorChartData get(int pos){
			return (VectorChartData)store.get(pos);
		}

		public void add(VectorChartData item){
			store.add(item);
		}

		public boolean remove(VectorChartData item){
			return store.remove(item);
		}

		public VectorChartData remove(int pos){
			return (VectorChartData) store.remove(pos);
		}

		public int size(){
			return store.size();
		}

//		public DefaultCategoryDataset toDataSet() {
//			DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
//			for (int i = 0;i < size();i++) {
//				for (int e = 0;e <get(i).size();e++) {
//					if (get(i).get(e).value != null) {
//						defaultcategorydataset.addValue(get(i).get(e).value, get(i).name, get(i).get(e).column);
//					}
//				}
//			}
//			return defaultcategorydataset;
//		}

	}
}

